<!DOCTYPE html>
<html>
<head>
    <title>notice form</title>
    <#include "/header.html">
</head>
<body class="bg-white">

<div id="appMain">
    <form class="layui-form layuimini-form" action="" lay-filter="dataForm">
        <div class="layui-form-item">
            <label class="layui-form-label">接收人</label>
            <div class="layui-input-block">
                <input type="radio" name="target" value="1" title="用户" checked>
                <input type="radio" name="target" value="2" title="店铺">
                <input type="radio" name="target" value="0" title="全部">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="required" lay-reqtext="标题不能为空" placeholder="输入标题" value="" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">内容</label>
            <div class="layui-input-block">
                <textarea name="content" lay-verify="required" lay-reqtext="内容不能为空" class="layui-textarea" placeholder="填写内容"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发送时间</label>
            <div class="layui-input-block">
                <input type="radio" name="sendTime" lay-filter="sendTime" value="1" title="即刻" checked>
                <input type="radio" name="sendTime" lay-filter="sendTime" value="2" title="定时">
            </div>
        </div>
        <div class="layui-form-item layui-hide" id="noticeTimeBox">
            <label class="layui-form-label">选择时间</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="noticeTime" id="test5" placeholder="点选时间">
            </div>
        </div>

        <div class="layui-form-item layui-hide">
            <div class="layui-input-block">
                <button type="button" class="layui-btn layui-btn-normal" id="commitBtn" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    layui.use(['form', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            $ = layui.$;

        var _parent = parent;
        if(_parent.formDataId > 0) {
            $.ajax({
                type: "POST",
                url: serverUrl('app/notice/info/' + _parent.formDataId),
                success: function (result) {
                    if (result.code === 0) {
                        var data = result.info;
                        if (data.status == 0) {
                            data['sendTime'] = 2;
                            $("#noticeTimeBox").removeClass('layui-hide');
                        }
                        data.noticeTime = data.noticeTime.substr(0, 16);
                        form.val('dataForm', result.info);
                    }
                }
            });
        }

        //日期时间选择器
        laydate.render({
            elem: '#test5'
            ,type: 'datetime'
            ,format: 'yyyy-MM-dd HH:mm'
        });

        form.on('radio(sendTime)', function(data){
            if (data.value == 2) {
                $("#noticeTimeBox").removeClass('layui-hide');
            } else {
                $("#noticeTimeBox").addClass('layui-hide');
            }
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var params = form.val('dataForm');
            params['id'] = _parent.formDataId || null;
            if (params.sendTime == 1) {
                params.noticeTime = '';
            }
            if (params.noticeTime != '') {
                params.noticeTime += ':00';
            }

            var method = _parent.formDataId > 0 ? 'update' : 'save';
            submitFormData('app/notice/'+method, params);

            return false;
        });

    });
</script>
</body>
</html>